<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="配置名称" prop="setName">
        <el-input
          v-model="queryParams.setName"
          placeholder="请输入配置名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否有效" prop="isEnable">
        <el-select v-model="queryParams.isEnable" placeholder="请选择是否有效" clearable>
          <el-option
            v-for="dict in dict.type.sys_is_enable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cms:sms-setup:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['cms:sms-setup:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['cms:sms-setup:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['cms:sms-setup:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="setList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="配置名称" align="center" prop="setName" />
      <el-table-column label="启用" align="center" prop="isEnable" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_enable" :value="scope.row.isEnable"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号限制" align="center">
        <el-table-column label="每分钟" align="center" prop="mobileLimitMinute" >
          <template slot-scope="scope">
            <span v-if="scope.row.mobileLimitMinute == -1">不限制</span>
            <div v-else>{{scope.row.mobileLimitMinute}} 条</div>
          </template>
        </el-table-column>
        <el-table-column label="每小时" align="center" prop="mobileLimitHour" >
          <template slot-scope="scope">
            <span v-if="scope.row.mobileLimitHour == -1">不限制</span>
            <div v-else>{{scope.row.mobileLimitHour}} 条</div>
          </template>
        </el-table-column>
        <el-table-column label="每个自然日" align="center" prop="mobileLimitDay" >
          <template slot-scope="scope">
            <span v-if="scope.row.mobileLimitDay == -1">不限制</span>
            <div v-else>{{scope.row.mobileLimitDay}} 条</div>
          </template>
        </el-table-column>
        <el-table-column label="启用" align="center" prop="mobileLimitState" width="80">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_is_enable" :value="scope.row.mobileLimitState"/>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="IP限制" align="center">
        <el-table-column label="每分钟" align="center" prop="ipLimitMinute" >
          <template slot-scope="scope">
            <span v-if="scope.row.ipLimitMinute == -1">不限制</span>
            <div v-else>{{scope.row.ipLimitMinute}} 条</div>
          </template>
        </el-table-column>
        <el-table-column label="每小时" align="center" prop="ipLimitHour" >
          <template slot-scope="scope">
            <span v-if="scope.row.ipLimitHour == -1">不限制</span>
            <div v-else>{{scope.row.ipLimitHour}} 条</div>
          </template>
        </el-table-column>
        <el-table-column label="每个自然日" align="center" prop="ipLimitDay" >
          <template slot-scope="scope">
            <span v-if="scope.row.ipLimitDay == -1">不限制</span>
            <div v-else>{{scope.row.ipLimitDay}} 条</div>
          </template>
        </el-table-column>
        <el-table-column label="启用" align="center" prop="ipLimitState" width="80">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_is_enable" :value="scope.row.ipLimitState"/>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cms:sms-setup:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cms:sms-setup:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改短信配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="配置名称" prop="setName">
              <el-input v-model="form.setName" placeholder="请输入配置名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否启用" prop="isEnable">
              <el-select v-model="form.isEnable" placeholder="请选择是否启用">
                <el-option
                  v-for="dict in dict.type.sys_is_enable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>



        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>手机号限制</span>
            <el-checkbox style="float: right; padding: 3px 0" v-model="form.mobileLimitState" :true-label="activeValue" :false-label="inactiveValue">启用</el-checkbox>
          </div>

          <el-row>
            <el-col :span="8">
              <el-form-item label="发送间隔" prop="mobileLimitMinute">
                <el-input type="number" min=-1 v-model="form.mobileLimitMinute" placeholder="请输入手机号限制额度" >
                  <template slot="append">秒</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="每小时" prop="mobileLimitHour">
                <el-input type="number" min=-1 v-model="form.mobileLimitHour" placeholder="请输入手机号限制额度" >
                  <template slot="append">条</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="每自然日" prop="mobileLimitDay">
                <el-input type="number" min=-1 v-model="form.mobileLimitDay" placeholder="请输入手机号限制额度" >
                  <template slot="append">条</template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>


        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>IP限制</span>
            <el-checkbox style="float: right; padding: 3px 0" v-model="form.ipLimitState" :true-label="activeValue" :false-label="inactiveValue">启用</el-checkbox>
          </div>

          <el-row>
            <el-col :span="8">
              <el-form-item label="发送间隔" prop="ipLimitMinute">
                <el-input type="number" min=-1 v-model="form.ipLimitMinute" placeholder="请输ip入手机号限制额度" >
                  <template slot="append">秒</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="每小时" prop="ipLimitHour">
                <el-input type="number" min=-1 v-model="form.ipLimitHour" placeholder="请输入ip限制额度" >
                  <template slot="append">条</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="每自然日" prop="ipLimitDay">
                <el-input type="number" min=-1 v-model="form.ipLimitDay" placeholder="请输入ip限制额度" >
                  <template slot="append">条</template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>

        <el-form-item label="配置备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
      <p style="color: red">注意：发送次数设置为-1代表不限制</p>
    </el-dialog>
  </div>
</template>

<script>
import { listSet, getSet, delSet, addSet, updateSet } from "@/api/cms/sms/set";

export default {
  name: "Set",
  dicts: ['sys_is_enable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 短信配置表格数据
      setList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        setName: null,
        isEnable: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      activeValue: 1,
      inactiveValue: 0
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询短信配置列表 */
    getList() {
      this.loading = true;
      listSet(this.queryParams).then(response => {
        this.setList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        setName: null,
        isEnable: 0,
        mobileLimitMinute: -1,
        mobileLimitHour: 1,
        mobileLimitDay: 1,
        mobileLimitState: 1,
        ipLimitMinute: -1,
        ipLimitHour: 1,
        ipLimitDay: 1,
        ipLimitState: 1,
        remark: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加短信配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSet(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改短信配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSet(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSet(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除短信配置编号为"' + ids + '"的数据项？').then(function() {
        return delSet(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sms/setup/export', {
        ...this.queryParams
      }, `set_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="scss" scoped>
.box-card {
  border: none;
  margin-bottom: 20px;
}
</style>
